#include<iostream>
#include<string.h>
#define MAX 1000
using namespace std;


void ShowSystem()//显示系统界面
{
	cout << "************************" << endl;
	cout << "**** 1.添加学生成绩 ****" << endl;
	cout << "**** 2.显示学生成绩 ****" << endl;
	cout << "**** 3.删除学生成绩 ****" << endl;
	cout << "**** 4.查找学生成绩 ****" << endl;
	cout << "**** 5.修改学生成绩 ****" << endl;
	cout << "**** 6.清空学生成绩 ****" << endl;
	cout << "**** 0.退出成绩系统 ****" << endl;
	cout << "************************" << endl;
}

struct Student	//学生结构体
{
	string Name;//姓名
	int Sex = 0;//性别 1 男 2 女
	int Age = 0;//年龄
	string Score;//成绩
};

struct AddSystem//成绩系统结构体
{
	//成绩系统中保存的学生数组
	struct Student StudentArray[MAX];
	//成绩系统当前的学生个数
	int Size = 0;
};

void AddStudent(AddSystem* S)//1.添加学生
{
	if (S->Size == MAX)
	{
		cout << "系统已满，无法添加！" << endl;
	}
	else {
		cout << "请输入姓名：" << endl;
		string name;
		cin >> name;
		S->StudentArray[S->Size].Name = name;

		cout << "请输入性别：" << endl;
		cout << "1  ---  男" << endl;
		cout << "2  ---  女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				S->StudentArray[S->Size].Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入（1 或 2）" << endl;
		}

		cout << "请输入年龄：" << endl;
		int age = 0;
		cin >> age;
		S->StudentArray[S->Size].Age = age;

		cout << "请输入成绩：" << endl;
		string score;
		cin >> score;
		S->StudentArray[S->Size].Score = score;

		//更新系统
		S->Size++;

		cout << "添加成功!" << endl;
		system("pause");//请按任意键继续
		system("CLS");//清屏
	}
}

void ShowStudent(AddSystem* S)//2.显示学生成绩
{
	//判断系统中是否为空
	if (S->Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else {
		for (int i = 0; i < S->Size; i++)
		{
			cout << "姓名：" << S->StudentArray[i].Name << "\t";
			cout << "性别：" << (S->StudentArray[i].Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄：" << S->StudentArray[i].Age << "\t";
			cout << "成绩：" << S->StudentArray[i].Score <<endl;
		}
	}
	system("pause");
	system("CLS");
}

int isExist(AddSystem* S, string name)//检测学生是否存在
{
	for (int i = 0; i < S->Size; i++)
	{
		//找到学生，并返回
		if (S->StudentArray[i].Name == name)
		{
			return i;
		}
	}
	return -1;
}

void DeleteStudent(AddSystem* S)//3.删除学生信息
{
	cout << "请输入您要删除的学生:" << endl;
	string name;
	cin >> name;

	//ret == -1 未查到
	//ret != -1 已查到
	int ret = isExist(S, name);
	if (ret == -1)
	{
		//查找到人,进行删除操作
		for (int i = ret; i < S->Size; i++)
		{
			//数据前移
			S->StudentArray[i] = S->StudentArray[i + 1];
		}
		//数据更新
		S->Size--;
		cout << "删除成功" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
}

void FindStudent(AddSystem* S)//4.查找学生信息
{
	cout << "请输入您所查找的学生" << endl;
	string name;
	cin >> name;
	int ret = isExist(S, name);
	if (ret != -1)
	{
		cout << "姓名：" << S->StudentArray[ret].Name << "\t";
		cout << "性别：" << S->StudentArray[ret].Sex << "\t";
		cout << "年龄：" << S->StudentArray[ret].Age << "\t";
		cout << "成绩：" << S->StudentArray[ret].Score<<endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

void MoidfyStudent(AddSystem* S)//5.修改学生信息
{
	cout << "请输入要修改的学生" << endl;
	string name;
	cin >> name;

	int ret = isExist(S, name);
	if (ret != -1)
	{
		cout << "请从新输入姓名：" << endl;
		string name;
		cin >> name;
		S->StudentArray[S->Size].Name = name;

		cout << "请输入性别：" << endl;
		cout << "1  ---  男" << endl;
		cout << "2  ---  女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				S->StudentArray[S->Size].Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入（1 或 2）" << endl;
		}

		cout << "请输入年龄：" << endl;
		int age = 0;
		cin >> age;
		S->StudentArray[S->Size].Age = age;

		cout << "请输入成绩：" << endl;
		string Score;
		cin >> Score;
		S->StudentArray[S->Size].Score = Score;
;
		cout << "修改成功" << endl;
	}
	else {
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

void CleanStudent(AddSystem* S)//6.清空学生系统
{
	S->Size = 0;
	cout << "学生已清空" << endl;
	system("pause");
	system("cls");
}

int main()
{
	AddSystem S;
	S.Size;

	int Judge;//创建用户选择的变量
	while (true) //while循环保证返回系统菜单
	{
		//目录调用
		ShowSystem();
		cout << "请输入你的选择：" << endl;
		cin >> Judge;
		switch (Judge)
		{
		case 1://添加学生
			AddStudent(&S);//利用地址传递,可以修饰实参
			break;
		case 2://显示学生
			ShowStudent(&S);
			break;
		case 3://删除学生
			DeleteStudent(&S);
			break;
		case 4://查找学生
			FindStudent(&S);
			break;
		case 5://修改学生
			MoidfyStudent(&S);
			break;
		case 6://清空学生
			CleanStudent(&S);
			break;
		case 0://退出系统
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	ShowSystem();
	system("pause");
	return 0;
}